#include <my_head.h>

void getsalt(const char *str, char *salt){
    int count = 0;
    const char *p = str;
    while(*p && count < 4){
        if(*p == '$'){
            count++;
        }
        p++;
    }
    if(count == 4){
        strncpy(salt, str, p - str);
        salt[p - str] = '\0';
    }
    else{
        salt[0] = '\0';
    }
}


int main() {
    const char *username = "bo";
    struct spwd *pwd = getspnam(username);
    if (pwd == NULL) {
        printf("查询出错或者账号不存在 \n");
        return 0;
    }
    printf("UserName: %s\n", pwd->sp_namp);
    printf("Password: %s\n", pwd->sp_pwdp);


    char salt[1024]={0};
    getsalt(pwd->sp_pwdp, salt);
    printf("%s\n", salt);
    char *encrypted = crypt("123", salt);
    printf("%s\n", encrypted);
    strcmp(pwd->sp_pwdp,encrypted) == 0 ? printf("相等\n"):printf("不相等\n");

    return 0;
}
